package com.light.mytext.http

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.light.mytext.R
import com.light.mytext.utiles.LoadUtils
import com.light.mytext.utiles.ToastUtils
import kotlinx.android.synthetic.main.activity_http.*
import net.idik.lib.slimadapter.SlimAdapter


class HttpActivity : AppCompatActivity() {
    var list_data = ArrayList<getjavaModel.DataBean.RecordsBean>()
    var mAdapter: SlimAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_http)
        init()
        init_Listener()
    }

    fun init() {
        LoadUtils.loading(this, null, recycle, false, LoadUtils.WindowCallBack {

        })
        mAdapter = SlimAdapter.create()
            .register<getjavaModel.DataBean.RecordsBean>(R.layout.text) { data, injector ->
                injector.text(R.id.tv_title, data.videoTitle)
            }.attachTo(recycle)
        mAdapter!!.updateData(list_data).notifyDataSetChanged()
    }

    fun init_Listener() {
        tv_get.setOnClickListener {
            getData()
        }
        tv_post.setOnClickListener {

        }
    }

    /**
     * 使用okhttp进行Get请求
     */
    fun getData() {
        var map = HashMap<String, String>()
        map["handlerName"] = "videoIndexHandler"
        map["page"] = "1"
        OKHttpRequestUtils(this, true).add(
            HttpIP.GetUrl,
            map,
            getjavaModel::class.java, HttpIP.Get,
            object : OKHttpRequestUtils.HttpCallBack<getjavaModel> {
                override fun onResponse(result: Boolean, message: String, data: Any?) {
                    var model = data as getjavaModel
                    list_data.clear()
                    list_data.addAll(model.data.records)
                    mAdapter!!.notifyDataSetChanged()
                }

                override fun onFailed(message: String) {
                    ToastUtils.show(this@HttpActivity, "失败")
                }

            })
    }


}